package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.order.models.bo.pay.PayOrderLogBO;
import com.ztgf.order.models.service.pay.IPayOrderLogService;
import com.ztgf.order.models.vo.pay.PayOrderLogVO;
import com.ztgf.shopping.models.bo.shop.ShopEarnestMoneyBO;
import com.ztgf.shopping.models.bo.shop.ShopEarnestMoneyReturnBO;
import com.ztgf.shopping.models.dto.shop.ShopEarnestMoneyDTO;
import com.ztgf.shopping.models.service.shop.IShopEarnestMoneyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 店铺保证金控制器
 *
 * @author zhanghualang
 * @Date 2020-06-16 14:38:24
 */

@RestController
@RequestMapping("/api/shopEarnestMoney")
@Slf4j
@Api(value = "ShopEarnestMoneyApiController",tags = "店铺保证金Api接口")
public class ShopEarnestMoneyApiController {

    @DubboReference
    private IShopEarnestMoneyService shopEarnestMoneyService;

    @DubboReference
    private IPayOrderLogService payOrderLogService;

    @ApiOperation(value = "根据店铺ID获取保证金信息",notes = "根据店铺ID获取保证金信息")
    @PostMapping(value = "/getByShopId")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result getByShopId(@RequestParam("id") Long id) {
        ShopEarnestMoneyDTO dto = shopEarnestMoneyService.getByShopId(id);
        BigDecimal sum = payOrderLogService.querySumDeductMarginMoney(id,9);
        if (dto != null){
            dto.setPayable (dto.getBalance ().add (dto.getVacancyAmount ()));
            dto.setUsedAmount (sum);
            return new Result<>(dto);
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }
    @ApiOperation(value = "查看保证金使用记录和退还保证金记录",notes = "查看保证金使用记录和退还保证金记录")
    @PostMapping(value = "/queryPagePayOrderLog")
    public Result<Page<PayOrderLogVO>> queryPagePayOrderLog(PayOrderLogBO bo) {

        if (bo == null || bo.getShopId () == null || bo.getType () == null){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<> (payOrderLogService.queryPagePayOrderLog(bo));

    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopEarnestMoneyDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopEarnestMoneyDTO dto = shopEarnestMoneyService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "店铺保证金列表",notes = "店铺保证金列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopEarnestMoneyDTO>> queryPageList(ShopEarnestMoneyBO bo){
        Wrapper<ShopEarnestMoneyDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<ShopEarnestMoneyDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = shopEarnestMoneyService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "余额缴纳保证金",notes = "余额缴纳保证金")
    @PostMapping(value = "/updatePaidByBalance")
    public Result updatePaidByBalance(Long id, String mobile, String captcha) {
        return shopEarnestMoneyService.updatePaidByBalance(id, mobile, captcha);
    }

    @ApiOperation(value = "网银缴纳保证金",notes = "网银缴纳保证金")
    @PostMapping(value = "/updatePaidByOnline")
    public Result updatePaidByOnline(Long id, String bankId) {
        return shopEarnestMoneyService.updatePaidByOnline(id, bankId);
    }

    @ApiOperation(value = "保证金缴纳回调接口",notes = "保证金缴纳回调接口")
    @PostMapping(value = "/updatePaidReturn")
    public Result updatePaidReturn(ShopEarnestMoneyReturnBO bo) {
        return shopEarnestMoneyService.updatePaidReturn(bo);
    }
}
